﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Cleware.Debit3.Tools.EdmUtil
{
    public class Logger
    {
        public enum LogType
        {
            Info,
            Skipped,
            Warning,
            Error
        }

        public static Logger Instance = new Logger();

        private Logger()
        {
        }

        public void Log(string message)
        {
            try
            {
                File.AppendAllText(Constants.LOGFILENAME, string.Format("{0}\t{1}\t{2}\n", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"), LogType.Info.ToString(),  message), Encoding.UTF8);
                Console.WriteLine(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("ERROR - log write failed\nException: \n{0}", ex.ToString()));
            }
        }
        public void Log(string message, Exception ex)
        {
            try
            {
                string exceptionAsString = ex == null ? "" : ex.ToString();
                File.AppendAllText(Constants.LOGFILENAME, string.Format("{0}\t{1}\t{2}, exception: {3}\n", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"), message, LogType.Info.ToString(), exceptionAsString), Encoding.UTF8);
                Console.WriteLine(message);
            }
            catch (Exception ex2)
            {
                Console.WriteLine(string.Format("ERROR - log write failed\nException: \n{0}", ex2.ToString()));
            }
        }
        public void Log(LogType logType, string message)
        {
            Log(logType, message, null);
        }
        public void Log(LogType logType, string message, Exception ex)
        {
            switch (logType)
            {
                case LogType.Skipped:
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    break;
                case LogType.Warning:
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    break;
                case LogType.Error:
                    Console.ForegroundColor = ConsoleColor.Red;
                    break;
                
                default:
                    break;
            }

            try
            {
                Console.WriteLine(message);
                if (ex != null)
                    message += ", exception: " + ex.ToString();
                File.AppendAllText(Constants.LOGFILENAME, string.Format("{0}\t{1}\t{2}\n", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"), message, logType.ToString(), message), Encoding.UTF8);
            }
            catch (Exception ex2)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(string.Format("ERROR - log write failed\nException: \n{0}", ex2.ToString()));
            }
            Console.ResetColor();
        }
    }
}
